﻿using App.Untiys;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Win.Dal;

namespace App.Desk.Sys.Setting
{
    public partial class FrmPwd : UIPage
    {
        public FrmPwd()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPwd.Text.Trim()))
            {
                txtPwd.Focus();
                UIMessageTip.ShowWarning("请输入原始密码！");
                return;
            }
            var currnetUser = SysUserServices.Info(new Model.SysUser { Acct = Config.CurrentUser.Acct });
            if (currnetUser.Pwd != DesEncrypt.Encrypt(txtPwd.Text.Trim()))
            {
                txtPwd.Focus();
                UIMessageTip.ShowWarning("原始密码错误！");
                return;
            }
            if (string.IsNullOrEmpty(txtNPwd.Text.Trim()))
            {
                txtNPwd.Focus();
                UIMessageTip.ShowWarning("请输入新密码！");
                return;
            }
            if (!DataTranf.IsPasswordComplex(txtNPwd.Text.Trim()))
            {
                txtNPwd.Focus();
                UIMessageTip.ShowWarning("新密码必须8位以上且包含字母大写、字母小写、数字、特殊字符！");
                return;
            }
            if (string.IsNullOrEmpty(txtCPwd.Text.Trim()))
            {
                txtCPwd.Focus();
                UIMessageTip.ShowWarning("请再次输入密码！");
                return;
            }
            if (txtNPwd.Text.Trim() != txtCPwd.Text.Trim())
            {
                txtCPwd.Focus();
                UIMessageTip.ShowWarning("两次输入密码不一致！");
                return;
            }
            if (SysUserServices.ChangePassword(new Model.SysUser { Id = Config.CurrentUser.Id, Pwd = DesEncrypt.Encrypt(txtCPwd.Text.Trim()) }))
            {
                UIMessageBox.ShowSuccess("密码修改成功！");
                txtNPwd.Text = "";
                txtPwd.Text = "";
                txtCPwd.Text = "";
            }
            else
                UIMessageBox.ShowError("密码修改失败！");
        }
    }
}
